package com.gitee.booting.dspi.autoconfigure;

import com.gitee.booting.dspi.processor.Parser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

public class DspiApplicationRunListener implements SpringApplicationRunListener {
    private static final String DSPI_ENVIRONMENT = "dspiMetaProperties";

    public DspiApplicationRunListener(SpringApplication application, String[] args) {
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        for (PropertySource<?> mps : environment.getPropertySources()) {
            if (DSPI_ENVIRONMENT.equals(mps.getName())) {
                return;
            }
        }
        environment.getPropertySources().addFirst(new MapPropertySource(DSPI_ENVIRONMENT, Parser.parse()));
    }
}
